/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *																 *
 *	CaseStudy1 - r2 Graphs Library Case Study 1					 *
 *																 *
 *	File:		Case1App.cpp									 *
 *	Authors:	Rafael Kikuchi ( shinjikik@gmail.com )			 *
 *				Rafael Romeiro ( rafaelromeiro@gmail.com )		 *
 *	Date:		15/09/2008										 *
 *	License:	CC-GNU LGPL										 *
 *				http://creativecommons.org/licenses/LGPL/2.1/	 *
 *	Version:	0.1.0											 *
 *																 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#include "Case1App.h"

void Case1App::init (void)
{
	cout << "Loading graph (using adj. matrix)...";
	clockMatrix[0] = clock();
	myGraphMatrix = new Graph("Case1Graph.txt", r2UNDIRECTED, r2MATRIX);
	clockMatrix[0] = clock() - clockMatrix[0];
	cout << " Done!" << endl;

	cout << "Loading graph (using adj. list)...";
	clockList[0] = clock();
	myGraphList = new Graph("Case1Graph.txt", r2UNDIRECTED, r2LIST);
	clockList[0] = clock() - clockList[0];
	cout << " Done!" << endl;
}

void Case1App::go (void)
{
	cout << "BFS using adj. matrix...";
	clockMatrix[1] = clock();
	myGraphMatrix->BFS(1);
	clockMatrix[1] = clock() - clockMatrix[1];
	cout << " Done!" << endl;

	cout << "BFS using adj. list...";
	clockList[1] = clock();
	myGraphList->BFS(1);
	clockList[1] = clock() - clockList[1];
	cout << " Done!" << endl;

	ofstream output("ListMatrixComparison.txt");
	if (output.is_open())
	{
		output << "Execution time and memory tests" << endl;
		output << "-------------------------------\n" << endl;
		output << "Using matrix:" << endl;
		output << "   Memory: " << myGraphMatrix->GetMemSize() << endl;
		output << "   Time: " << endl;
		output << "     Create object: " << double(clockMatrix[0])/CLOCKS_PER_SEC << endl;
		output << "     BFS: " << double(clockMatrix[1])/CLOCKS_PER_SEC << endl;
		output << "     Total: " << double(clockMatrix[0] + clockMatrix[1])/CLOCKS_PER_SEC << endl;
		output << endl;

		output << "Using list:" << endl;
		output << "   Memory: " << myGraphList->GetMemSize() << endl;
		output << "     Create object: " << double(clockList[0])/CLOCKS_PER_SEC << endl;
		output << "     BFS: " << double(clockList[1])/CLOCKS_PER_SEC << endl;
		output << "     Total: " << double(clockList[0] + clockList[1])/CLOCKS_PER_SEC << endl;

		output.close();
	}

	cout << "Writing BFS tree...";
	myGraphList->WriteBFSTree("Case1Graph_bfs.txt", 1);
	cout << " Done!" << endl;

	cout << "Writing DFS tree...";
	myGraphList->WriteDFSTree("Case1Graph_dfs.txt", 1);
	cout << " Done!" << endl;

	cout << "Writing components...";
	myGraphList->WriteConnComponents("Case1Graph_components.txt");
	cout << " Done!" << endl;
}

void Case1App::release (void)
{
	delete myGraphMatrix;
	delete myGraphList;
}
